<?php

namespace app\zyhsapi\model\comment;

use app\zyhsapi\model\BaseCacheZyhs;
use XznsTools\cache\zyhs\CacheManageZyhs;

class ZyhsArticleMyInteracted extends BaseCacheZyhs
{
    protected $table = 'good_wechat.zyhs_article_my_interacted';

    protected $cacheModel = 'zyhs.content_my_interacted';

    /**
     * 获取 用户 我参与的 分页数据
     * @param int $uid
     * @param int $aType 1=>平行宇宙
     * @return array
     * @throws \Exception
     */
    public function getMyInteractedPageList(int $uid, int $aType, int $page, int $limit = 10): array
    {
        return CacheManageZyhs::getInstance()
            ->setCacheConfig($this->cacheModel, $uid)
            ->setListKeyType($aType)// 类型 用于分页 例如 0全部 1待支付 已支付 //
            ->setPage($page)
            ->setPageSize($limit)
            ->setSort('desc')
            ->setListCacheTime(86400)    // 一天 过期时间
            ->getListPageDataIds(function ($isCache = true, $sortType = 'desc') use ($uid, $aType)
            {
                try
                {
                    $data = $this->where(['uid' => $uid, 'atype' => $aType])->column('interaction_time', 'aid');
                    $resData = [];
                    foreach ($data as $id => $interactionTime)
                    {
                        $resData[$id] = ['interaction_time' => $interactionTime];
                    }

                    return $resData;
                }
                catch (\Exception $e)
                {
                    return [];
                }
            }, 'interaction_time', 0);
    }

    /**
     * 获取 用户 我参与的 内容ID
     * @param int $uid
     * @param int $aType 1=>平行宇宙
     * @return array
     * @throws \Exception
     */
    public function getMyInteractedAids(int $uid, int $aType): array
    {
        $resData = $this->getMyInteractedPageList($uid, $aType, -1);

        return $resData['data'] ? array_keys($resData['data']) : [];
    }

    /**
     * 设置 我参与的
     * @param int $uid
     * @param int $aType
     * @param int $aid
     * @param int $commentId 评论ID
     * @param int $crId 评论回复ID
     * @return void
     * @throws \Exception
     */
    public function setMyInteracted(int $uid, int $aType, int $aid, int $commentId, int $crId = 0)
    {
        $myInteracted = $this->getMyInteractedAids($uid, $aType);
        if (in_array($aid, $myInteracted, true))
        {
            $this->updateMyInteracted($uid, $aType, $aid, $commentId, $crId);
            return;
        }
        $data = [
            'uid' => $uid,
            'atype' => $aType,
            'aid' => $aid,
            'comment_id' => $commentId,
            'cr_id' => $crId,
            'interaction_time' => time(),
        ];
        $this->addMyInteracted($data);
    }

    /**
     * 添加 我参与的
     * @param array $data
     * @return bool
     */
    public function addMyInteracted(array $data)
    {
        try
        {
            $this->save($data);

            $this->listDataAdd($data['uid'], [$data['aid'] => $data['interaction_time']], $data['atype']);
            return true;
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return false;
        }
    }

    /**
     * 更新 我参与的
     * @param int $uid
     * @param int $aType
     * @param int $aid
     * @param int $commentId
     * @param int $crId
     * @return bool
     */
    public function updateMyInteracted(int $uid, int $aType, int $aid, int $commentId, int $crId = 0)
    {
        try
        {
            $nowTime = time();
            $this->where(['uid' => $uid, 'atype' => $aType, 'aid' => $aid])->update([
                'comment_id' => $commentId,
                'cr_id' => $crId,
                'interaction_time' => $nowTime,
            ]);

            $this->listDataAdd($uid, [$aid => $nowTime], $aType);
            return true;
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return false;
        }
    }

    /**
     * 取消 我参与的
     * @param array $data
     * @return bool
     */
    public function delMyInteracted(array $data)
    {
        try
        {
            $this->where(['uid' => $data['uid'], 'aid' => $data['aid'], 'atype' => $data['atype']])->delete();

            $this->listDataDel($data['uid'], [$data['aid']], $data['atype']);
            return true;
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return false;
        }
    }
}